# These options can be changed

srcdirs := src 
incdirs := include
bindir := bin
bin := tp1_prog_systeme
objdir := make.cache
makedir := make.cache
outputversion := 1.0
useversion := n

# executable shared_lib static_lib
outputmode := executable

CXX := gcc
CPPFLAGS :=
CXXFLAGS := -Wall -pedantic -g
LDFLAGS :=

# Filter input
srcdirs := $(srcdirs)
incdirs := $(incdirs)
bindir := $(bindir)
objdir := $(objdir)

# Not these ones
srcs := $(shell find $(srcdirs) -type f -name '*.c')
objs := $(addprefix $(objdir)/, $(srcs:.c=.o))
deps := $(addprefix $(makedir)/, $(srcs:.c=.d))

# Output name
ifeq ($(useversion), y)

output_executable := $(addprefix $(bindir)/, $(basename $(bin))-$(outputversion)$(suffix $(bin)))
output_static_lib := $(addprefix $(bindir)/, lib$(basename $(bin))-$(outputversion)$(suffix $(bin)).a)
ep_objs_file := $(addprefix $(makedir)/, $(basename $(bin))-$(outputversion)$(suffix $(bin)).entrypoint)

else

output_executable := $(addprefix $(bindir)/, $(bin))
output_static_lib := $(addprefix $(bindir)/, lib$(bin).a)
ep_objs_file := $(addprefix $(makedir)/, $(bin).entrypoint)

endif


#EXECUTABLE
ifeq ($(outputmode),executable)

all: $(output_executable)
	@echo "$(output_executable) Done."
	
$(output_executable): $(objs) | $(bindir)
	@echo Linking...
	@$(CXX) -o $@ $+ $(LDFLAGS)

test: $(output_executable)
	./$(output_executable)
	
endif

# STATIC LIB
ifeq ($(outputmode),static_lib)

all: $(output_static_lib)
	@echo "$(output_static_lib) Done."

$(output_static_lib): $(objs) | $(bindir)
	@rm -f $(output_static_lib) $(ep_objs_file)
	@echo Creating archive $(notdir $(output_static_lib))...
	@for i in $(objs); do \
		test -n "$$(nm $$i | grep -e '^[0-9a-z]* T main$$')" && echo $$i >> $(ep_objs_file) || ar -cq $(output_static_lib) $$i;\
	done
	@echo Making executable $(notdir $(output_executable))...
	@$(CXX) -o $(output_executable) `cat $(ep_objs_file)` $(LDFLAGS) -l$(subst $(bindir)/lib,,$(basename $(output_static_lib))) -L$(bindir) 


#dum:
#	@echo -l$(subst $(bindir)/lib,,$(basename $(output_static_lib)))
#	@echo $(ep_objs_file)

#$(output_static_lib): $(output_static_lib)($(objs))
#	$(CXX) -o $(output_executable) $(LDFLAGS) `cat $(ep_objs_file)` -l$(subst $(bindir)/lib,,$(basename $(output_static_lib))) -L$(dir $(output_static_lib))

#$(output_static_lib)(%.o): %.o | $(bindir)
#	@test -n "$$(nm $< | grep -e '^[0-9a-z]* T main$$')" && echo $< >> $(ep_objs_file) || ar -csq $(output_static_lib) $<; \

endif


$(objdir)/%.o: %.c | $(objdir)
	@mkdir -p $(dir $@)
	@echo Compiling $(notdir $<)...
	@$(CXX) -c $< -o $@ -I$(incdirs) $(CXXFLAGS) $(CPPFLAGS)

$(makedir)/%.d: %.c | $(makedir)
	@echo Making $(notdir $<) dependencies...
	@mkdir -p $(dir $@)
	@rm -f $@
	@$(CXX) -MM -I$(incdirs) $(CXXFLAGS) $(CPPFLAGS) $< > $@.temp
	@sed 's,\(.*\.o\)[ :]*, $(objdir)/$*.o $@ : ,g' < $@.temp > $@
	@rm -f $@.temp


ifneq ($(MAKECMDGOALS),clean)
-include $(deps)
endif

.PHONY: clean

$(bindir):
	@mkdir $(bindir)

ifneq ($(bindir),$(objdir))
$(objdir):
	@mkdir $(objdir)
endif

ifneq ($(bindir),$(makedir))
ifneq ($(objdir),$(makedir))
$(makedir):
	@mkdir $(makedir)
endif
endif

clean:
	@rm -rf $(objs) $(wildcard $(bindir)/*$(bin)*) $(wildcard $(makedir)/*$(bin)*)  $(deps)
	@[ -e $(objdir) ] && [ $(objdir) != . ] && find $(objdir) -depth -empty -type d -exec rmdir {} \; || :
	@[ -e $(bindir) ] && [ $(bindir) != "." ] && find $(bindir) -depth -empty -type d -exec rmdir {} \; || :
	@[ -e $(makedir) ] && [ $(makedir) != "." ] && find $(makedir) -depth -empty -type d -exec rmdir {} \; || :
	@echo Cleaned.
